package cn.itcast.hotel.mq;

import cn.itcast.hotel.constants.HotelMqConstants;
import cn.itcast.hotel.service.IHotelService;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @Author: 生如夏花
 * @Date: 2022/3/16 11:10
 */
@Component
public class HotelMq {

    @Autowired
    private RabbitTemplate rabbitTemplate;
    @Autowired
    private IHotelService hotelService;

    /**
     * 消费mq新增和更新消息
     * @param id
     */
    @RabbitListener(bindings = @QueueBinding (
            value = @Queue(name = HotelMqConstants.INSERT_QUEUE_NAME),
            exchange = @Exchange(name = HotelMqConstants.EXCHANGE_NAME,type = ExchangeTypes.TOPIC),
            key = HotelMqConstants.INSERT_KEY
    ))
    public void listInsertOrUpdateQueue(Long id){
        hotelService.InsertOrUpdateById(id);
    }

    /**
     * 消费mq删除消息
     * @param id
     */
    @RabbitListener(bindings = @QueueBinding (
            value = @Queue(name = HotelMqConstants.DELETE_QUEUE_NAME),
            exchange = @Exchange(name = HotelMqConstants.EXCHANGE_NAME,type = ExchangeTypes.TOPIC),
            key = HotelMqConstants.DELETE_KEY
    ))
    public void listDeleteQueue(Long id){
        hotelService.deleteById(id);
    }
}
